from pydantic import BaseModel, Field
from datetime import datetime
from typing import Optional

class TransactionResponse(BaseModel):
    """交易记录响应模型"""
    id: str
    trans_code: str
    trans_sn: str
    rrn: Optional[str]
    orderid: str
    pan: str
    trans_amt: Optional[str]
    trans_date: str
    trans_time: str
    merno: str
    terminal: str
    resp_code: str
    bank_date: str
    org_trans_sn: Optional[str]
    org_rrn: Optional[str]
    pay_order: Optional[str]  # 添加外部订单号字段
    created_at: Optional[datetime]

    class Config:
        from_attributes = True 

class FESRequest(BaseModel):
    trans_code: str = Field(default="FES", description="交易代码")
    appid: str = Field(max_length=3, description="交易来源")
    orderid: str = Field(max_length=30, description="订单流水")
    trans_date: str = Field(max_length=8, description="交易日期")
    trans_time: str = Field(max_length=6, description="交易时间")
    org_trans_code: str = Field(max_length=3, description="待查询交易类型")
    org_pay_order: str = Field(max_length=20, description="待查询外部订单号")
    merno: str = Field(max_length=15, description="商户号")
    terminal: str = Field(max_length=8, description="终端号")
    md5: Optional[str] = Field(None, description="大写MD5值")
    
    # 这个字段不会发送到卡系统，只用于后端查询
    org_pay_order: Optional[str] = Field(None, max_length=20, description="原交易外部订单号")

    class Config:
        json_schema_extra = {
            "example": {
                "appid": "01",
                "orderid": "FES202507110001",
                "org_trans_code": "CEP",
                "org_pay_order": "TEST202507119184",
                "merno": "999999999999997",
                "terminal": "99999996"
            }
        }

class FESResponse(BaseModel):
    trans_code: str = Field(default="FES", description="交易代码")
    bank_date: str = Field(max_length=8, description="清算日期")
    resp_code: str = Field(max_length=5, description="响应码")
    orderid: str = Field(max_length=30, description="请求订单流水")
    org_orderid: Optional[str] = Field(None, max_length=30, description="待查询订单号")
    org_trans_sn: Optional[str] = Field(None, max_length=30, description="待查询卡系统流水")
    org_resp_code: Optional[str] = Field(None, max_length=5, description="待查询响应码")
    org_rrn: Optional[str] = Field(None, max_length=12, description="待查询系统参考号")
    pan: Optional[str] = Field(None, max_length=19, description="待查询卡号") 